package com.badshun.ui;

import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.utils.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.*;
import com.badshun.player.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.math.*;
import com.badlogic.gdx.physics.box2d.*;
import com.badshun.stage.*;
import com.badshun.box2d.*;
import com.badshun.skill.*;

public class GameUI {
	public ImageButton leftButton;
	public ImageButton rightButton;
	public ImageButton upButton;
	public ImageButton downButton;
	
	public ImageButton actionButton;
	
	private MainPlayer mainPlayer;
	private NPCPlayersManager npm;
	private NPCPlayers npcs;
	private OrthographicCamera camera;
	private Box2DWorld world;
	
	public GameUI(MainPlayer mainPlayer,NPCPlayersManager npm,OrthographicCamera camera,Box2DWorld world) {
		this.mainPlayer = mainPlayer;
		this.npm = npm;
		this.camera = camera;
		this.world = world;
		
		defineImageButton();
	}

	public void setNpcs() {
		if(npm.npcs != null) {
			this.npcs = npm.npcs;
		}
	}

	
	private void defineImageButton() {
		leftButton = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/left.jpg")))));
		rightButton = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/right.jpg")))));
		upButton = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/up.jpg")))));
		downButton = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/down.jpg")))));
		
		actionButton = new ImageButton(new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("ui/atkbutton.jpg")))));
		
		leftButton.setPosition(100,150);
		rightButton.setPosition(250,150);
		upButton.setPosition(150 + rightButton.getWidth() / 2,200);
		downButton.setPosition(150 + rightButton.getWidth() / 2,88);
		
		actionButton.setPosition(Gdx.graphics.getWidth() - 200,150);
		
		setListener();
	}
	
	private void setListener() {
		leftButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.MOVE;
				
				mainPlayer.playerBody.setType(BodyDef.BodyType.DynamicBody);
				
				if(mainPlayer.playerBody.getLinearVelocity().x > -151) {
					mainPlayer.playerBody.applyLinearImpulse(new Vector2(-150,0),mainPlayer.playerBody.getWorldCenter(),true);
				}
				
				return true;
			}
			
			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.IDLE;
				mainPlayer.idle = MainPlayer.IDLE.IDLE_LEFT;
				
				mainPlayer.playerBody.applyLinearImpulse(new Vector2(150,0),mainPlayer.playerBody.getWorldCenter(),true);
				mainPlayer.playerBody.setType(BodyDef.BodyType.StaticBody);
			}
		});
		
		rightButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.MOVE;
				
				mainPlayer.playerBody.setType(BodyDef.BodyType.DynamicBody);
				
				if(mainPlayer.playerBody.getLinearVelocity().x < 151) {
					mainPlayer.playerBody.applyLinearImpulse(new Vector2(150,0),mainPlayer.playerBody.getWorldCenter(),true);
				}
				
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.IDLE;
				mainPlayer.idle = MainPlayer.IDLE.IDLE_RIGHT;
				
				mainPlayer.playerBody.applyLinearImpulse(new Vector2(-150,0),mainPlayer.playerBody.getWorldCenter(),true);
				mainPlayer.playerBody.setType(BodyDef.BodyType.StaticBody);
			}
		});
			
		upButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.MOVE;
				
				mainPlayer.playerBody.setType(BodyDef.BodyType.DynamicBody);
				
				if(mainPlayer.playerBody.getLinearVelocity().y < 151) {
					mainPlayer.playerBody.applyLinearImpulse(new Vector2(0,150),mainPlayer.playerBody.getWorldCenter(),true);
				}
				
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.IDLE;
				mainPlayer.idle = MainPlayer.IDLE.IDLE_UP;
				
				mainPlayer.playerBody.applyLinearImpulse(new Vector2(0,-150),mainPlayer.playerBody.getWorldCenter(),true);
				mainPlayer.playerBody.setType(BodyDef.BodyType.StaticBody);
			}
		});
			
		downButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.MOVE;
				
				mainPlayer.playerBody.setType(BodyDef.BodyType.DynamicBody);
				
				if(mainPlayer.playerBody.getLinearVelocity().y > -151) {
					mainPlayer.playerBody.applyLinearImpulse(new Vector2(0,-150),mainPlayer.playerBody.getWorldCenter(),true);
				}
				
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				mainPlayer.action = MainPlayer.ACTION.IDLE;
				mainPlayer.idle = MainPlayer.IDLE.IDLE_DOWN;
				
				mainPlayer.playerBody.applyLinearImpulse(new Vector2(0,150),mainPlayer.playerBody.getWorldCenter(),true);
				mainPlayer.playerBody.setType(BodyDef.BodyType.StaticBody);
			}
		});
		
		actionButton.addListener(new InputListener() {
			@Override
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
				for(int i = 0; i < npcs.list.getLength(); i++) {
					NPCPlayer npc = npcs.list.getChild(i);
					
					
					if(mainPlayer.isHitNPC && npc.isHitPlayer) {
						switch(mainPlayer.direction) {
							case UP:
								npc.direction = NPCPlayer.DIRECTION.DOWN;
								
								break;
							case DOWN:
								npc.direction = NPCPlayer.DIRECTION.UP;
								
								break;
							case LEFT:
								npc.direction = NPCPlayer.DIRECTION.RIGHT;

								break;
							case RIGHT:
								npc.direction = NPCPlayer.DIRECTION.LEFT;

								break;
						}
						
						npc.next();
					}
				}
				
				if(mainPlayer.isHitDoor && mainPlayer.hiter.equals(MainCityStage.TAG)) {
					mainPlayer.esm.createMcr1s(mainPlayer,npm,camera,world);
					
					world.setContactListener(mainPlayer,mainPlayer.esm.mcr1s.npm.npcs,mainPlayer.esm);

					mainPlayer.resetPlayer(new Vector2(160,32),MainPlayer.DIRECTION.UP);
				} else if(mainPlayer.isHitDoor && mainPlayer.hiter.equals(MainCityRoom1Stage.TAG)) {
					mainPlayer.esm.createMcs(mainPlayer,npm,camera,world);
					
					world.setContactListener(mainPlayer,mainPlayer.esm.mcs.npm.npcs,mainPlayer.esm);

					mainPlayer.resetPlayer(new Vector2(384,448),MainPlayer.DIRECTION.DOWN);
				}
				
				else {
					mainPlayer.createSkill_BigFire(mainPlayer);
				}
				
				return true;
			}

			@Override
			public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
				
			}
		});
	}
	
	public void update() {
		if(mainPlayer.esm.stage != null) {
			if((mainPlayer.esm.stage instanceof MainCityStage) && mainPlayer.esm.mcs.isActive) {
				setNpcs();
				
				mainPlayer.esm.mcs.isActive = false;
			} else if((mainPlayer.esm.stage instanceof MainCityRoom1Stage) && mainPlayer.esm.mcr1s.isActive) {
				setNpcs();

				mainPlayer.esm.mcr1s.isActive = false;
			}
		}
	}
}
